package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/courses")
@Slf4j
@RestController
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 添加课程
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加课程{}",course);
        courseService.add(course);
        return Result.success();
    }

    /**
     * 获取课程列表
     */
    @GetMapping
    public Result listCourse(String name, Integer subject,Integer target,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("获取课程列表,参数:name={},subject={} ,target={},page={},pageSize={}",name,subject,target,page,pageSize);
        PageResult<Course> pageResult = courseService.listCourse(name,subject,target, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 根据ID查询课程回显
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据ID查询课程回显,参数:id={}",id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    /**
     * 查询所有课程
     */
    @GetMapping("/list")
    public Result list() {
        log.info("查询所有课程");
        PageResult<Course> pageResult = courseService.list();
        return Result.success(pageResult);
    }


    /**
     * 修改课程
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改课程 course: {}", course);
        courseService.update(course);
        return Result.success();
    }

    /**
     * 删除课程
     */
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程 id: {}", id);
        courseService.deleteById(id);
        return Result.success();
    }

    /**
     * 根据学科查询课程
     */
    @GetMapping("/subject/{subject}")
    public Result listSubject( @PathVariable Integer subject) {
        log.info("根据学科查询课程,参数:subject={}",subject);
        List<Course> list = courseService.listSubject(subject);
        return Result.success(list);
    }
}
